import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../common/macro.dart';

abstract class BaseState<T extends StatefulWidget> extends State<T> {
  late String title = "";
  late Color barColor = Colors.white;
  late Color backgroundColor = colorf5f5f5;
  final RefreshController refreshController =
      RefreshController(initialRefresh: false);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: backgroundColor,
      appBar: AppBar(
        backgroundColor: barColor,
        centerTitle: true,
        title: Text(
          title,
          style: const TextStyle(
            color: Color(0xff333333),
            fontWeight: FontWeight.bold,
            fontSize: 18,
          ),
        ),
      ),
      body: initBody(context),
    );
  }

  Widget initBody(BuildContext context);

  Widget addRefresher(VoidCallback onRefresh, Widget child,
      {VoidCallback? onLoading}) {
    return SmartRefresher(
      enablePullDown: true,
      enablePullUp: onLoading != null,
      header: const WaterDropHeader(),
      footer: onLoading != null
          ? CustomFooter(
              builder: (BuildContext context, LoadStatus? mode) {
                Widget body;
                if (mode == LoadStatus.idle) {
                  body = const Text("上拉加载");
                } else if (mode == LoadStatus.loading) {
                  body = const CupertinoActivityIndicator();
                } else if (mode == LoadStatus.failed) {
                  body = const Text("加载失败！点击重试！");
                } else if (mode == LoadStatus.canLoading) {
                  body = const Text("松手,加载更多!");
                } else {
                  body = const Text("没有更多数据了!");
                }

                return SizedBox(
                  height: 55.0,
                  child: Center(child: body),
                );
              },
            )
          : null,
      controller: refreshController,
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: child,
    );
  }
}
